<?php
include_once("../fram/frameFunction.php");
include_once("../layo/layoutFunction.php");
/*************************************************************************
 * 
 * 		FUNZIONI RELATIVE AL PROCESSO
 * 
 **************************************************************************/

/**
 * inserisce un nuovo processo
 */
 function puIns($redirOnEnd = true){
 global $objSDAO;
	$errMsg = "";
	if(isset($_REQUEST["process_unit"]) && ereg("[^a-zA-Z0-9_]",$_REQUEST["process_unit"]))
		$errMsg .= _t("Il nome processo deve essere un alfanumerico").".<br>";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if($errMsg != ""){ return $errMsg; }		
	
	//controllo che il processo non esista gi@
	$module = 	trim(sxREQUEST("module"));
	$sub_module = 	trim(sxREQUEST("sub_module"));
	$process_unit = 	"$module.$sub_module.".trim(sxREQUEST("process_unit"));
	$sSQL = "SELECT count(*) as num FROM process_unit WHERE process_unit='".sxAS($process_unit)."'";
	$num = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= _t("Il processo")." '$process_unit' "._t("esiste gi&agrave;.");
	if($errMsg != ""){ return $errMsg; }		
	
	$sSQL = "INSERT INTO " .
			"process_unit " .
			"(process_unit, " .
			"description, " .
			"module, " .
			"sub_module, " .
			"type, " .
			"note)" .
			"VALUES('".sxAS($process_unit)."', " .
			"'".trim(sxRAS("description"))."', " .
			"'".sxAS($module)."', " .
			"'".sxAS($sub_module)."', " .
			"'".trim(sxRAS("type"))."', " .
			"'".trim(sxRAS("note"))."')";	
	$objSDAO->exec($sSQL);
	
	$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
			" VALUES('".sxAS($process_unit)."','W','','variabili di memoria','','N')";
	$objSDAO->exec($sSQL);
	$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
			" VALUES('".sxAS($process_unit)."','X','','array 1 livello','','N')";
	$objSDAO->exec($sSQL);
	$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
			" VALUES('".sxAS($process_unit)."','Y','','array 2 livelli','','N')";
	$objSDAO->exec($sSQL);
	
	if(insertDoc($process_unit, $errMsg)){
		logRecord("300", _t("Inserito nuovo processo")." '".$process_unit."'");
		if($redirOnEnd){
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}
	}
	return $errMsg;
}
 
/**
 * ELIMINA un processo
 */
function puDel(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	$process_unit = $_REQUEST["process_unit"];
	$module = $_REQUEST["module"];
	$sub_module = $_REQUEST["sub_module"];
		
	if($errMsg != ""){return $errMsg;}
	
	//tabelle dipendenti
	$aTabDip['entry_point'][] = 'process_unit';
	$aTabDip['entry_point_alias'][] = 'process_unit';
	$aTabDip['entry_point_alias_match'][] = 'process_unit';
	$aTabDip['entry_point_attribute'][] = 'process_unit';
	$aTabDip['entry_point_rule'][] = 'process_unit';
	$aTabDip['entry_point_rule_attribute'][] = 'process_unit';
	$aTabDip['entry_point_rule_sub_rule'][] = 'process_unit';
	$aTabDip['entry_point_variable'][] = 'process_unit';
	$aTabDip['frame'][] = 'process_unit';
	$aTabDip['frame_alias_match'][] = 'process_unit';
	$aTabDip['menu_line'][] = 'process_unit';
	$aTabDip['process_unit_doc'][] = 'process_unit';
	$aTabDip['pu_alias'][] = 'process_unit';
	$aTabDip['pu_attribute'][] = 'process_unit';
	$aTabDip['pu_link'][] = 'process_unit';
	$aTabDip['pu_variable'][] = 'process_unit';
	$aTabDip['process_unit'][] = 'process_unit';
	$aTabDip['pu_link'][] = 'pu_linked';
	//elimino i dati dal db
	foreach($aTabDip as $tabX => $aNomeX) {
		foreach($aNomeX as $nomeX) {
			$sSQL = "DELETE FROM $tabX WHERE $nomeX = '$process_unit'";
			$objSDAO->query($sSQL, "del");
		}
	}
	//elimino i documenti associati al processo
	$dir = SERVER_FS_DOC."/mb_module";
	$dir .= "/".substr($process_unit,0,4);
	$dir .= "/".substr($process_unit,5,4);
	$dir .= "/".substr($process_unit,10);		
	if ($dh = opendir($dir)) {	
		while($fileX = readdir($dh)){
			if($fileX[0]=='.') continue;
			@unlink($dir."/".$fileX);
		}
		closedir($dh);
		@rmdir($dir);
	}
	logRecord("500", _t("Eliminato processo")." '".sxREQUEST("pu")."'");
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return $errMsg;
}

/**
 * Modifica un processo
 */
function puMod(){
	global $objSDAO;
	$errMsg = "";	$process_unit = $_REQUEST["process_unit"];
	$module = $_REQUEST["module"];
	$sub_module = $_REQUEST["sub_module"];
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	
	if(trim($_REQUEST["description"]) == "")
		$errMsg .= _t("La description &egrave; obbligatoria")."<br>";
	
	$new_process_unit = substr($_REQUEST["process_unit"],0,10).$_REQUEST["new_name"];
	if ($new_process_unit != $process_unit) {
		$sSQL = "SELECT count(*) as num FROM process_unit " .
				"	WHERE process_unit='".sxAS($new_process_unit)."'";
		$num = 0;
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		//return $sSQL." - (".$row["num"].")";
		if($row["num"] != 0)
			$errMsg .= _t("Il processo")." '$new_process_unit' "._t("esiste gi&agrave;.");
	}	
	if($errMsg != ""){
		return $errMsg;
	}
	$sSQL = "UPDATE process_unit " .
			"SET description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
			"type='".addslashes(sxGPC(trim($_REQUEST["type"])))."', " .
			"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
			"WHERE process_unit='".addslashes(sxGPC($_REQUEST["process_unit"]))."'";	
	$objSDAO->query($sSQL, "mod");
	if ($new_process_unit != $process_unit) {
		$aTabDip['entry_point'][] = 'process_unit';
		$aTabDip['entry_point_alias'][] = 'process_unit';
		$aTabDip['entry_point_alias_match'][] = 'process_unit';
		$aTabDip['entry_point_attribute'][] = 'process_unit';
		$aTabDip['entry_point_rule'][] = 'process_unit';
		$aTabDip['entry_point_rule_attribute'][] = 'process_unit';
		$aTabDip['entry_point_rule_sub_rule'][] = 'process_unit';
		$aTabDip['entry_point_variable'][] = 'process_unit';
		$aTabDip['frame'][] = 'process_unit';
		$aTabDip['frame_alias_match'][] = 'process_unit';
		$aTabDip['menu_line'][] = 'process_unit';
		$aTabDip['process_unit_doc'][] = 'process_unit';
		$aTabDip['pu_alias'][] = 'process_unit';
		$aTabDip['pu_attribute'][] = 'process_unit';
		$aTabDip['pu_link'][] = 'process_unit';
		$aTabDip['pu_variable'][] = 'process_unit';
		$aTabDip['process_unit'][] = 'process_unit';
		$aTabDip['pu_link'][] = 'pu_linked';
		//elimino i dati dal db
		foreach($aTabDip as $tabX => $aNomeX) {
			foreach($aNomeX as $nomeX) {
				$sSQL = "UPDATE $tabX SET $nomeX = '$new_process_unit' WHERE $nomeX = '$process_unit'";
				//$errMsg .= $sSQL."<br>";
				$objSDAO->query($sSQL, "del");
			}
		}
	}
	$module = sxREQUEST("module");
	if(!deleteDoc($process_unit, $errMsg) or !insertDoc($process_unit, $errMsg)){
		$errMsg .= _t("Fallito caricamento dei documenti")."<br>";
	}
	if ($errMsg != '') {
		return $errMsg;
	}
	$_SESSION[USEROBJ]->reLoad();
	logRecord("400", _t("Modificato processo")." '".$process_unit."'");
	//serverRedir("puView.php?pu=$process_unit&xsn=".XSN);
	$_SESSION[USEROBJ]->setHistoryVar(array('pu'=>$new_process_unit));
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return "errore redirect";
}

/*************************************************************************
 * 
 * 		FUNZIONI RELATIVE AI LINK
 * 
 **************************************************************************/

/**
 * inserisce un nuovo link
 */
 function puLinkIns($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	$_REQUEST['name'] = trim(strtolower($_REQUEST['name']));
	if(sxREQUEST("name") == "")
		$errMsg .= _t("Il nome &egrave; obbligatorio").".<br>";
	if(ereg("[^a-z0-9_]",sxREQUEST("name")))
		$errMsg .= _t("Il nome deve essere un alfanumerico").".<br>";
	if(!isset($_REQUEST["pu_linked"]) || $_REQUEST["pu_linked"] == "")
		$errMsg .= _t("Il processo associato &egrave; obbligatorio").".<br>";
	if(isset($_REQUEST["sequence"]) && ereg("[^0-9]",$_REQUEST["sequence"]))
		$errMsg .= _t("La sequenza deve essere un numero").".<br>";
	
	//controllo che il link non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_link WHERE process_unit='".$_REQUEST["process_unit"]."' AND name='".$_REQUEST["name"]."'";
	$num = 0;
	if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("test")){
			$num = $row["num"]; 
		}
	}else{
		$errMsg .= $objSDAO->error;
	}
	if($num != 0)
		$errMsg .= _t("Il link")." '".$_REQUEST["name"]."' "._t("a processo esiste gi&agrave;.")."<br>";
	if($errMsg == ""){		
		$sequence = addslashes(sxGPC(trim($_REQUEST["sequence"])));
		if($sequence == ""){
			$sequence = 0;
		}
		if (trim(sxREQUEST('label'))=='') $_REQUEST['label'] = $_REQUEST['name'];
		$sSQL = "INSERT INTO " .
				"pu_link " .
				"(process_unit, " .
				"name, " .
				"pu_linked, " .
				$quote."group$quote, " .
				"sequence, " .
				"label, " .
				"tooltip, " .
				"note)" .
				"VALUES('".sxRAS("process_unit")."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["name"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["pu_linked"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["group"])))."', " .
				"".$sequence.", " .
				"'".addslashes(sxGPC(trim($_REQUEST["label"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["tooltip"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$process_unit = sxREQUEST("process_unit");
			$_SESSION[USEROBJ]->reLoad();
			logRecord("300", _t("Inserito nuovo link al processo")." '".$process_unit."'");
			if($redirOnEnd){
				//serverRedir("puView.php?pu=$process_unit&xsn=".XSN);
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);	
			}		
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}

/**
 * elimina un link processo
 */
 function puLinkDel(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["name"]) || $_REQUEST["name"] == "")
		$errMsg .= _t("Il nome &egrave; obbligatorio").".<br>";
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$name = addslashes(sxGPC(trim($_REQUEST["name"])));
	if($errMsg == ""){
		$sSQL = "DELETE FROM pu_link " .
				"WHERE process_unit='$process_unit' AND name='$name'";	
		if($objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
			//elimino i profili associati
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Eliminato link")." '$name' "._t("dal processo")." '$process_unit'");
			//serverRedir("puView.php?pu=$process_unit&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 }
 
 
/**
 * aggiorna un link processo
 */
 function puLinkMod(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	$_REQUEST["new_name"] = strtolower($_REQUEST["new_name"]);
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(ereg("[^a-z0-9_]",sxREQUEST("name")))
		$errMsg .= _t("Il nome deve essere un alfanumerico").".<br>";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$name = addslashes(sxGPC(trim($_REQUEST["name"])));

	//controllo che il link non esista gi@
	if (sxREQUEST('new_name') != sxREQUEST('name')) {
		$sSQL = "SELECT count(*) as num FROM pu_link " .
				"	WHERE process_unit='".sxRAS('process_unit')."' " .
						"	AND name='".sxRAS('new_name')."'";
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row['num'] != 0)
			$errMsg .= _t("Il link a processo esiste gi&agrave;.")."<br>";
	}

	if($errMsg != ""){return $errMsg;}
	$sequence = addslashes(sxGPC(trim($_REQUEST["sequence"])));
	if($sequence == ""){
		$sequence = 0;
	}
	if (trim(sxREQUEST('label'))=='') $_REQUEST['label'] = $_REQUEST['name'];
	$sSQL = "UPDATE pu_link SET " .
			"pu_linked='".addslashes(sxGPC(trim($_REQUEST["pu_linked"])))."', " .
			$quote."group$quote='".addslashes(sxGPC(trim($_REQUEST["group"])))."', " .
			"sequence=".$sequence.", " .
			"label='".addslashes(sxGPC(trim($_REQUEST["label"])))."', " .
			"tooltip='".addslashes(sxGPC(trim($_REQUEST["tooltip"])))."', " .
			"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."' " .
			"WHERE process_unit='$process_unit' AND name='$name'";	
	$objSDAO->query($sSQL,"upd");
	if (sxREQUEST('new_name') != sxREQUEST('name')) {
		$sSQL = "UPDATE pu_link SET name = '".sxRAS('new_name')."' " .
				"	WHERE process_unit='".sxRAS('process_unit')."' " .
				"  	AND name='".sxRAS('name')."'";
		$objSDAO->query($sSQL,"test");
	}
	logRecord("500", _t("Aggiornato link")." '$name' "._t("dal processo")." '$process_unit'");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return $errMsg;
 }
 
 /*************************************************************************
 * 
 * 		FUNZIONI RELATIVE AGLI ALIAS
 * 
 **************************************************************************/

/**
 * inserisce un nuovo alias
 */
 function puAliasIns($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	if(sxREQUEST("entity") == "" and sxREQUEST("alias") < "W")
		$errMsg .= _t("L'entity &egrave; obbligatoria").".<br>";
	if(sxREQUEST("entity") != "" and sxREQUEST("alias") >= "W")
		$errMsg .= _t("L'entity non deve essere definita").".<br>";
	
	
	//controllo che l'alias non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_alias WHERE process_unit='".$_REQUEST["process_unit"]."' AND alias='".$_REQUEST["alias"]."'";
	$num = 0;
	if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("test")){
			$num = $row["num"]; 
		}
	}else{
		$errMsg .= $objSDAO->error;
	}
	if($num != 0)
		$errMsg .= _t("L'alias")." '".$_REQUEST["alias"]."' "._t("esiste gi&agrave;.")."<br>";
	if($errMsg == ""){		
		$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
		$sSQL = "INSERT INTO " .
				"pu_alias " .
				"(process_unit, " .
				"alias, " .
				"entity, " .
				"description, " .
				"read_only, " .
				"note)" .
				"VALUES('".sxRAS("process_unit")."', " .
				"'".strtoupper(addslashes(sxGPC(trim($_REQUEST["alias"]))))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["entity"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"'$read_only', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$process_unit = sxREQUEST("process_unit");
			$_SESSION[USEROBJ]->reLoad();
			logRecord("300", _t("Inserito nuovo alias nel processo")." '".$process_unit."'");
			//serverRedir("puAliasList.php?pu=$process_unit&xsn=".XSN);
			if($redirOnEnd){
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);
			}			
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}

/**
 * elimina un alias
 */
 function puAliasDel(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	if($errMsg == ""){
		$sSQL = "DELETE FROM pu_alias " .
				"WHERE process_unit='$process_unit' AND alias='$alias'";	
		if($objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
			//elimino le variabili associati
			$sSQL = "DELETE FROM pu_variable " .
				"WHERE process_unit='$process_unit' AND alias='$alias'";	
			if(!$objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
				$errMsg .= "Elimina variabile associate ad alias: ".$sSQL."<br> - ".$objSDAO->error;
			}
			//elimino gli attributi associati
			$sSQL = "DELETE FROM pu_attribute " .
				"WHERE process_unit='$process_unit' AND alias='$alias'";	
			if(!$objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
				$errMsg .= "Elimina attribute associato ad alias: ".$sSQL."<br> - ".$objSDAO->error;
			}
			//elimino i match associati all'alias
			$sSQL = "DELETE FROM frame_alias_match " .
				"WHERE process_unit='$process_unit' AND alias='$alias'";	
			if(!$objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
				$errMsg .= "Elimina match associato ad alias: ".$sSQL."<br> - ".$objSDAO->error;
			}
			if($errMsg == ""){
				$_SESSION[USEROBJ]->reLoad();
				logRecord("500", _t("Eliminato l'alias")." '$alias' "._t("dal processo")." '$process_unit'");
				//serverRedir("puAliasList.php?pu=$process_unit&xsn=".XSN);
				$url = $_SESSION[USEROBJ]->getHistory(-1, true);
				serverRedir($url);
			}
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 } 
 
/**
 * aggiorna un alias
 */
 function puAliasMod(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(sxREQUEST("entity") == "" and sxREQUEST("alias") < "W")
		$errMsg .= _t("L'entity @ obbligatoria").".<br>";
	if(sxREQUEST("entity") != "" and sxREQUEST("alias") > "Q")
		$errMsg .= _t("L'entity non deve essere definita").".<br>";
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	if($errMsg == ""){
		$read_only = isset($_REQUEST["read_only"])?"Y":"N";
		$sSQL = "UPDATE pu_alias SET " .
				"entity='".addslashes(sxGPC(trim($_REQUEST["entity"])))."', " .
				"description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"read_only='$read_only', " .
				"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."' " .
				"WHERE process_unit='$process_unit' AND alias='$alias'";	
		if($objSDAO->query($sSQL,"upd",array("contOnErr"=>true))){
			//elimino i profili associati
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Aggiornato l'alias")." '$alias' "._t("dal processo")." '$process_unit'");
			//serverRedir("puAliasList.php?pu=$process_unit&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 }
 
 /*************************************************************************
 * 
 * 		FUNZIONI RELATIVE AGLI ATTRIBUTE
 * 
 **************************************************************************/

/**
 * elimina un attribute
 */
 function puAttrDel(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	$entity = addslashes(sxGPC(trim($_REQUEST["entity"])));
	$attribute = addslashes(sxGPC(trim($_REQUEST["attribute"])));
	if($errMsg == ""){
		$sSQL = "DELETE FROM pu_attribute " .
				"WHERE process_unit='$process_unit' " .
				"AND entity='$entity'" .
				"AND attribute='$attribute'" .
				"AND alias='$alias'";	
		if($objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
			//elimino i profili associati
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Eliminato l'attribute")." '$attribute' "._t("dal processo")." '$process_unit'");
			//serverRedir("puAttrList.php?pu=$process_unit&alias=$alias&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 } 
 
/**
 * inserisce o aggiorna un attribute
 */
 function puAttrMod($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	$entity = addslashes(sxGPC(trim($_REQUEST["entity"])));
	$attribute = addslashes(sxGPC(trim($_REQUEST["attribute"])));
	if($errMsg != ""){ return $errMsg; }
	
	$read_only = isset($_REQUEST["read_only"])?"Y":"N";
	//controllo che l'attribiuto non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_attribute " .
			"WHERE process_unit='".$_REQUEST["process_unit"]."' " .
			"AND entity='".$_REQUEST["entity"]."'" . 
			"AND attribute='".$_REQUEST["attribute"]."'" . 
			"AND alias='".$_REQUEST["alias"]."'";
	$num = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row['num'] == 0) {
		$sSQL = "INSERT INTO " .
				"pu_attribute " .
				"(process_unit, " .
				"alias, " .
				"entity, " .
				"attribute, " .
				"description, " .
				"read_only, " .
				"note)" .
				"VALUES('".sxRAS("process_unit")."', " .
				"'".strtoupper(addslashes(sxGPC(trim($_REQUEST["alias"]))))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["entity"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["attribute"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"'$read_only', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		$objSDAO->query($sSQL,"ins");
		$process_unit = addslashes(sxGPC($_REQUEST["process_unit"]));
		$alias = addslashes(sxGPC($_REQUEST["alias"]));
		logRecord("300", _t("Inserito nuovo attribute nel processo")." '".$process_unit."'");
		if($redirOnEnd){
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}		
		
	}
	$sSQL = "UPDATE pu_attribute SET " .
			"description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
			"read_only='$read_only', " .
			"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."' " .
			"WHERE process_unit='$process_unit' " .
			"AND entity='$entity'" .
			"AND attribute='$attribute'" .
			"AND alias='$alias'";	
	$objSDAO->query($sSQL,"upd");
	logRecord("500", _t("Aggiornato l'attribute")." '$attribute' "._t("dal processo")." '$process_unit'");
	if($redirOnEnd){
		$url = $_SESSION[USEROBJ]->getHistory(0, true);
		serverRedir($url);
	}
 }
 
 
  /*************************************************************************
 * 
 * 		FUNZIONI RELATIVE ALLE VARIABILI
 * 
 **************************************************************************/

/**
 * inserisce una nuova variabile
 */
 function puVarIns($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["variable"]) || $_REQUEST["variable"] == "")
		$errMsg .= _t("La variabile &egrave; obbligatoria").".<br>";
	if(isset($_REQUEST["variable"]) && ereg("[^a-zA-Z 0-9 - _]",$_REQUEST["variable"]))
		$errMsg .= _t("La variable deve essere un alfanumerico").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	if(isset($_REQUEST["alias"]) && ereg("[^a-zA-Z 0-9 - _]",$_REQUEST["alias"]))
		$errMsg .= _t("L'alias deve essere un alfanumerico").".<br>";
	
	
	//controllo che il link non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_variable " .
			"WHERE process_unit='".$_REQUEST["process_unit"]."' " .
			"AND variable='".$_REQUEST["variable"]."'" . 
			"AND alias='".$_REQUEST["alias"]."'";
	$num = 0;
	if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("test")){
			$num = $row["num"]; 
		}
	}else{
		$errMsg .= $objSDAO->error;
	}
	if($num != 0)
		$errMsg .= _t("La variabile")." '".$_REQUEST["variable"]."' "._t("esiste gi&agrave;.")."<br>";
	if($errMsg == ""){		
		$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
		$sSQL = "INSERT INTO " .
				"pu_variable " .
				"(process_unit, " .
				"alias, " .
				"variable, " .
				"description, " .
				"read_only, " .
				"note)" .
				"VALUES('".addslashes(sxGPC($_REQUEST["process_unit"]))."', " .
				"'".strtoupper(addslashes(sxGPC(trim($_REQUEST["alias"]))))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["variable"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"'$read_only', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$process_unit = addslashes(sxGPC($_REQUEST["process_unit"]));
			$alias = addslashes(sxGPC($_REQUEST["alias"]));
			$_SESSION[USEROBJ]->reLoad();
			logRecord("300", _t("Inserito nuova variabile nel processo")." '".$process_unit."'");
			//serverRedir("puVarList.php?pu=$process_unit&alias=$alias&xsn=".XSN);
			if($redirOnEnd){
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);
			}
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}

/**
 * elimina una variabile
 */
 function puVarDel(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["variable"]) || $_REQUEST["variable"] == "")
		$errMsg .= _t("L'variable &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	if(isset($_REQUEST["entity"])){
		$entity = addslashes(sxGPC(trim($_REQUEST["entity"])));
	}else{
		$entity = "";
	}
	$variable = addslashes(sxGPC(trim($_REQUEST["variable"])));
	if($errMsg == ""){
		$sSQL = "DELETE FROM pu_variable " .
				"WHERE process_unit='$process_unit' " .
				"AND variable='$variable'" .
				"AND alias='$alias'";	
		if($objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
			//elimino i profili associati
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Eliminato la variable")." '$variable' "._t("dal processo")." '$process_unit'");
			//serverRedir("puVarList.php?pu=$process_unit&alias=$alias&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 } 
 
/**
 * aggiorna una variabile
 */
 function puVarMod(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["variable"]) || $_REQUEST["variable"] == "")
		$errMsg .= _t("L'variable &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	$variable = addslashes(sxGPC(trim($_REQUEST["variable"])));
	if($errMsg == ""){
		$read_only = isset($_REQUEST["read_only"])?"Y":"N";
		$sSQL = "UPDATE pu_variable SET " .
				"description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"read_only='$read_only', " .
				"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."' " .
				"WHERE process_unit='$process_unit' " .
				"AND variable='$variable'" .
				"AND alias='$alias'";	
		if($objSDAO->query($sSQL,"upd",array("contOnErr"=>true))){
			//elimino i profili associati
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Aggiornato la variable")." '$variable' "._t("dal processo")." '$process_unit'");
			//serverRedir("puVarList.php?pu=$process_unit&alias=$alias&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 }
 
 
 /*************************************************************************
 * 
 * 		FUNZIONI RELATIVE AI DOCUMENTI
 * 
 **************************************************************************/
 
 /**
 * 
 * gestione degli allegati ad un processo: 
 * 
 */
function insertDoc($process_unit, &$errMsg){
	global $objSDAO;
	$fileName = "";
	$name = "";
	//leggo il documento
	if(isset($_FILES["document"]) && $_FILES["document"]["error"] == 0 && $_FILES["document"]["size"] != 0){
		//verifico che il doc non esista gi@
		$fileName = elabFileName($_FILES["document"]["name"]);
		$name = addslashes(sxGPC(trim($_REQUEST["doc_name"])));
		if($name == ""){
			$name = $fileName;
		}
		$sSQL = "SELECT count(*) AS numDoc FROM process_unit_doc 
				WHERE process_unit='$process_unit' AND file_name='$fileName'";
		//colloco il file nella cartella documenti
		if(!$objSDAO->query($sSQL, "count", array("contOnErr"=>true))){
			$errMsg .=  $sSQL."<br> - ".$objSDAO->error;
		}else{
			$rowCount=$objSDAO->fetch("count");
			if($rowCount && $rowCount["numDoc"] == 0){
				$tmpFile = $_FILES["document"]["tmp_name"];
				if(!file_exists(SERVER_FS_DOC)){
					$errMsg .= _t("La cartella per i documenti '%s' non esiste!",SERVER_FS_DOC)."<br>";
					return false;
				}
				$dir = SERVER_FS_DOC."/mb_module";
				if(!file_exists($dir)){
					mkdir($dir,0775);
				}
				$dir .= "/".substr($process_unit,0,4);
				if(!file_exists($dir)){
					mkdir($dir,0775);
				}
				$dir .= "/".substr($process_unit,5,4);
				if(!file_exists($dir)){
					mkdir($dir,0775);
				}
				$dir .= "/".substr($process_unit,10);
				if(!file_exists($dir)){
					mkdir($dir,0775);
				}
				if(!copy($tmpFile, $dir."/$fileName")){
					$errMsg .= _t("Non &egrave; stato possibile copiare correttamente il documento! (%s)",$dir."/$fileName")."<br>";
					return false;
				}
				
				$sSQL = "INSERT INTO process_unit_doc 
						(process_unit, file_name, name, mime_type)
						VALUES
						('".addslashes(sxGPC(trim($process_unit)))."', '$fileName', '$name', '".$_FILES["document"]["type"]."')";
				if(!$objSDAO->query($sSQL,"upd",array("contOnErr"=>true))){
					$errMsg .=  $sSQL."<br> - ".$objSDAO->error;
				}
				if($errMsg != "")				
					return false;
			}else{
				$errMsg .= _t("Il documento")." $fileName "._t(" &egrave; gi&agrave; associato al modulo")." $process_unit<br>";
				return false;
			}
		}
	}
	return true;
}

/**
 * ripulisce il fileName originale
 */
 function elabFileName($nomeFile){
 		$elabFileName = ereg_replace('[^a-zA-Z 0-9 . - _]', "_", str_replace(" ", "_", $nomeFile));
 		return $elabFileName;
 }
 
 /**
  * elimina gli allegati del processo
  */
 function deleteDoc($process_unit, &$errMsg){
 	global $objSDAO;
	$fileName = "";
	$name = "";
	foreach($_REQUEST as $key=>$value){
		if(strpos($key, "elim_") !== false && strtolower($value) == "on"){
			$fileName = $_REQUEST["fn_$key"];
			$dir = SERVER_FS_DOC."/mb_module";
			$dir .= "/".substr($process_unit,0,4);
			$dir .= "/".substr($process_unit,5,4);
			$dir .= "/".substr($process_unit,10);			
			if(file_exists($dir."/$fileName")){
				unlink($dir."/$fileName");
			}				
			$sSQL = "DELETE FROM process_unit_doc WHERE process_unit='$process_unit' AND file_name='$fileName'";
			$objSDAO->query($sSQL, "del");
		}
	}
	return true;
 }
 
 /**
  * esporta le process_unit selezionate
  */
 function puExp(){
 	global $objSDAO;
 	$sMsg = "";
 	$numPu = 0;
 	$aPU = array();
 	//carico i processi da esportare in $aPU
 	foreach($_REQUEST as $key=>$value){
 		if(strpos($key, "exp_") !== false){
 			$numPu ++;
 			$aPU[$value] = array();
 			$aPUExp[] = $value;
 		}
 	}
 	if($numPu == 0){
 		$sMsg = _t("Nessun processo &egrave; stato selezionato.");
 	}else{
 		foreach($aPU as $process_unit=>$aTmp){
 			//carico i dati del processo
 			$sSQL = "SELECT * FROM process_unit WHERE process_unit='$process_unit'"; 			
 			if($objSDAO->query($sSQL, "pu")){
	 			while($row = $objSDAO->fetch("pu")){
	 					$aPU[$process_unit]["process_unit"] = array("description"=>$row["description"],
	 																							"note"=>$row["note"],
	 																							"module"=>$row["module"],
	 																							"sub_module"=>$row["sub_module"],
	 																							"type"=>$row["type"]);
	 			} 			
 			}else{
 				$sMsg .= "ERRORE di Esportreqtype processo $process_unit<br>".$objSDAO->error;
				return $sMsg;
 			}	
 			//carico i frame associati al processo
 			$sSQL = "SELECT * FROM frame WHERE process_unit='$process_unit'";
 			if($objSDAO->query($sSQL, "frame")){
 				$aPU[$process_unit]["frames"] = array();
 				while($row = $objSDAO->fetch("frame")){
 					$aPU[$process_unit]["frames"][$row["frame"]] = array("description"=>$row["description"],
 																							"note"=>$row["note"],
 																							"display_only"=>$row["display_only"]);
 					//carico il layout set
 					$sSQL = "SELECT * FROM layout_set WHERE layout_set='".$row["layout_set"]."'";
 					if($objSDAO->query($sSQL, "layset")){
 						while($rowLS = $objSDAO->fetch("layset")){
 							$aPU[$process_unit]["frames"][$row["frame"]]["layout_set"] = array("layout_set"=>$rowLS["layout_set"],
 																											"module"=>$rowLS["module"],
 																											"description"=>$rowLS["description"],
 																											"note"=>$rowLS["note"],
 																											"type"=>$rowLS["type"],
 																											"max_colspan"=>$rowLS["max_colspan"]);
 							//carico i layout associati al layout_set
 							$sSQL = "SELECT * FROM layout WHERE layout_set='".$row["layout_set"]."'";
		 					if($objSDAO->query($sSQL, "lay")){
		 						while($rowLay = $objSDAO->fetch("lay")){
 									$aPU[$process_unit]
 									["frames"][$row["frame"]]
 									["layout_set"]["layouts"][$rowLay["layout"]] = array("flags"=>$rowLay["flags"],
 																											"description"=>$rowLay["description"],
 																											"note"=>$rowLay["note"],
 																											"display_only"=>$rowLay["display_only"]);
		 							//carico le band associate al layout
		 							$sSQL = "SELECT * FROM layout_band WHERE layout_set='".$row["layout_set"]."' AND layout='".$rowLay["layout"]."'";
		 							if($objSDAO->query($sSQL, "band")){
				 						while($rowBand = $objSDAO->fetch("band")){
		 									$aPU[$process_unit]
		 									["frames"][$row["frame"]]
		 									["layout_set"]["layouts"][$rowLay["layout"]]
		 									["bands"][$rowBand["band"]] = array("flags"=>$rowBand["flags"],
		 																					"description"=>$rowBand["description"],
		 																					"note"=>$rowBand["note"],
 																							"display_only"=>$rowLay["display_only"]);
				 							//carico i field associate alla band
				 							$sSQL = "SELECT * FROM layout_field " .
				 									"WHERE layout_set='".$row["layout_set"]."' " .
				 											"AND layout='".$rowLay["layout"]."'" .
				 													"AND band='".$rowBand["band"]."'";
				 							if($objSDAO->query($sSQL, "field")){
						 						while($rowField = $objSDAO->fetch("field")){
				 									$aPU[$process_unit]
				 									["frames"][$row["frame"]]
				 									["layout_set"]["layouts"][$rowLay["layout"]]
				 									["bands"][$rowBand["band"]]
				 									["fields"][$rowField["row"]."_".$rowField["seq"]] = array("row"=>$rowField["row"],
				 																					"seq"=>$rowField["seq"],
				 																					"layout_alias"=>$rowField["layout_alias"],
				 																					"entity"=>$rowField["entity"],
				 																					"attribute"=>$rowField["attribute"],
				 																					"variable"=>$rowField["variable"],
				 																					"label"=>$rowField["label"],
				 																					"label_attr"=>$rowField["label_attr"],
				 																					"label_align"=>$rowField["label_align"],
				 																					"label_style"=>$rowField["label_style"],
				 																					"field_type"=>$rowField["field_type"],
				 																					"field_value_list"=>$rowField["field_value_list"],
				 																					"field_attr"=>$rowField["field_attr"],
				 																					"field_align"=>$rowField["field_align"],
				 																					"field_style"=>$rowField["field_style"],
				 																					"field_width"=>$rowField["field_width"],
				 																					"field_height"=>$rowField["field_height"],
				 																					"check_variable"=>$rowField["check_variable"],
				 																					"html_string"=>$rowField["html_string"],
				 																					"field_colspan"=>$rowField["field_colspan"],
				 																					"label_colspan"=>$rowField["label_colspan"],
				 																					"layout_set"=>$rowField["layout_set"],
				 																					"field_mode"=>$rowField["field_mode"],
				 																					"field_check"=>$rowField["field_check"],
				 																					"field_format"=>$rowField["field_format"],
				 																					"field_format_par"=>$rowField["field_format_par"],
				 																					"lookup_attr1"=>$rowField["lookup_attr1"],
				 																					"lookup_attr2"=>$rowField["lookup_attr2"],
				 																					"lookup_attr3"=>$rowField["lookup_attr3"],
				 																					"lookup_sep1"=>$rowField["lookup_sep1"],
				 																					"lookup_sep2"=>$rowField["lookup_sep2"],
				 																					"lookup_sep3"=>$rowField["lookup_sep3"],
				 																					"lookup_sep4"=>$rowField["lookup_sep4"]);
						 						}
						 					}else{
								 				$sMsg .= "ERRORE di Esportreqtype campi di ".$rowBand["band"]."<br>".$objSDAO->error;
								 				return $sMsg;
								 			}					 							
				 						}
				 					}else{
						 				$sMsg .= "ERRORE di Esportreqtype band di ".$rowLay["layout"]."<br>".$objSDAO->error;
						 				return $sMsg;
						 			}		 							
		 						}
		 					}else{
				 				$sMsg .= "ERRORE di Esportreqtype layout di ".$rowLS["layout_set"]."<br>".$objSDAO->error;
				 				return $sMsg;
				 			}
		 					//carico gli alias associati al layout_set
		 					$sSQL = "SELECT * FROM layout_alias WHERE layout_set='".$row["layout_set"]."'";
		 					if($objSDAO->query($sSQL, "layalias")){
		 						while($rowLayAlias = $objSDAO->fetch("layalias")){
 									$aPU[$process_unit]
 											["frames"][$row["frame"]]
 											["layout_set"]["aliases"][$rowLayAlias["layout_alias"]] = array("alias"=>$rowLayAlias["layout_alias"], "entity"=>$rowLayAlias["entity"]);
			 						//carico le variabili associate all'alias
			 						$sSQL = "SELECT * FROM layout_variable WHERE layout_set='".$row["layout_set"]."' AND layout_alias='".$rowLayAlias["layout_alias"]."'";
				 					if($objSDAO->query($sSQL, "aliasVar")){
				 						while($rowAliasVar = $objSDAO->fetch("aliasVar")){
		 									$aPU[$process_unit]
		 											["frames"][$row["frame"]]
		 											["layout_set"]["aliases"][$rowLayAlias["layout_alias"]]
		 											["variables"][$rowAliasVar["variable"]] = array("domain"=>$rowAliasVar["domain"],
		 																											"length"=>$rowAliasVar["length"],
		 																											"decimals"=>$rowAliasVar["decimals"]);
				 						}
			 						}else{
						 				$sMsg .= "ERRORE di Esportreqtype variabili di ".$rowLayAlias["layout_alias"]."<br>".$objSDAO->error;
						 				return $sMsg;
						 			}
						 			//carico il matching fra alias di layout e gli alias di processo
						 			$sSQL = "SELECT * FROM frame_alias_match " .
						 					"WHERE process_unit='".$row["process_unit"]."' " .
						 							"AND frame='".$row["frame"]."' " .
							 							"AND layout_set='".$rowLayAlias["layout_set"]."' " .
							 									"AND layout_alias='".$rowLayAlias["layout_alias"]."'";
				 					if($objSDAO->query($sSQL, "match")){
				 						while($rowMatch = $objSDAO->fetch("match")){
		 									$aPU[$process_unit]
		 											["frames"][$row["frame"]]
		 											["layout_set"]["aliases"][$rowLayAlias["layout_alias"]]
		 											["matching"][$rowMatch["alias"]] = array("alias"=>$rowMatch["alias"]);
				 						}
			 						}else{
						 				$sMsg .= "ERRORE di Esportreqtype matching alias di ".$rowLay["layout_alias"]."<br>".$objSDAO->error;
						 				return $sMsg;
						 			}
		 						}
		 					}else{
				 				$sMsg .= "ERRORE di Esportreqtype alias di ".$rowLS["layout_set"]."<br>".$objSDAO->error;
				 				return $sMsg;
				 			}
 						}
 					}else{
		 				$sMsg .= "ERRORE di Esportreqtype layout set di ".$row["frame"]."<br>".$objSDAO->error;
		 				return $sMsg;
		 			}		 			
 				}
 			}else{
 				$sMsg .= "ERRORE di Esportreqtype frame di ".$process_unit."<br>".$objSDAO->error;
 				return $sMsg;
 			}
 			//carico i pu_alias
 			$sSQL = "SELECT * FROM pu_alias WHERE process_unit='$process_unit'";
			if($objSDAO->query($sSQL, "pual")){
				while($rowAl = $objSDAO->fetch("pual")){
					$aPU[$process_unit]["aliases"][$rowAl["alias"]] = array("entity"=>$rowAl["entity"],
																							"description"=>$rowAl["description"],
																							"note"=>$rowAl["note"],
																							"read_only"=>$rowAl["read_only"]);
					//carico le variabili collegate a quest'alias
					$sSQL = "SELECT * FROM pu_variable WHERE process_unit='$process_unit' AND alias='".$rowAl["alias"]."'";
					if($objSDAO->query($sSQL, "puvar")){
						while($rowVar = $objSDAO->fetch("puval")){
							$aPU[$process_unit]["aliases"][$rowAl["alias"]]
							["variables"][$rowVar["variable"]] = array("description"=>$rowVar["description"],
																					"note"=>$rowVar["note"],
																					"read_only"=>$rowVar["read_only"]);						
						}
					}else{
		 				$sMsg .= "ERRORE di Esportreqtype variable di ".$rowAl["alias"]."<br>".$objSDAO->error;
		 				return $sMsg;
		 			}
					//carico gli attributi collegati a quest'alias
					$sSQL = "SELECT * FROM pu_attribute WHERE process_unit='$process_unit' AND alias='".$rowAl["alias"]."'";
					if($objSDAO->query($sSQL, "puatt")){
						while($rowAttr = $objSDAO->fetch("puatt")){
							$aPU[$process_unit]["aliases"][$rowAl["alias"]]
							["attributes"]
							[$rowAttr["entity"]." ".$rowAttr["attribute"]] = array("entity"=>$rowAttr["entity"], 
																					"attribute"=>$rowAttr["attribute"],
																					"description"=>$rowAttr["description"],
																					"note"=>$rowAttr["note"],
																					"read_only"=>$rowAttr["read_only"]);						
						}
					}else{
		 				$sMsg .= "ERRORE di Esportreqtype attribute di ".$rowAl["alias"]."<br>".$objSDAO->error;
		 				return $sMsg;
		 			}																						
				}
			}else{
 				$sMsg .= "ERRORE di Esportreqtype alias di $process_unit<br>".$objSDAO->error;
 				return $sMsg;
 			}
 			//carico i link associati al processo
 			$sSQL = "SELECT * FROM pu_link WHERE process_unit='$process_unit'";
 			if($objSDAO->query($sSQL, "link")){
 				$aPU[$process_unit]["links"] = array();
 				while($row = $objSDAO->fetch("link")){
 					$aPU[$process_unit]["links"][$row["name"]] = array("pu_linked"=>$row["pu_linked"],
 																							"group"=>$row["group"],
 																							"sequence"=>$row["sequence"],
 																							"label"=>$row["label"],
 																							"tooltip"=>$row["tooltip"],
 																							"note"=>$row["note"]);
 				}
 			}else{
 				$sMsg .= "ERRORE di Esportreqtype link di $process_unit<br>".$objSDAO->error;
 				return $sMsg;
 			}
 			//carico i documenti associati al processo
 			$sSQL = "SELECT * FROM process_unit_doc WHERE process_unit='$process_unit'";
 			if($objSDAO->query($sSQL, "doc")){
 				$aPU[$process_unit]["docs"] = array();
 				while($row = $objSDAO->fetch("doc")){
 					$aPU[$process_unit]["docs"][$row["file_name"]] = array("mime_type"=>$row["mime_type"],
 																							"name"=>$row["name"]);
 				}
 			}else{
 				$sMsg .= "ERRORE di Esportreqtype link di $process_unit<br>".$objSDAO->error;
 				return $sMsg;
 			}
 		}
 		$sPU = serialize($aPU);
 		$fh = fopen("./puExport.txt", "wb");
 		fwrite($fh, $sPU);
 		fclose($fh);
 		$sMsg = _t("L'esportreqtype di")." ".implode(", ", $aPUExp)." "._t("&egrave; stata eseguita con successo");
 		$sMsg .= "<br><a target=\"_blank\" href=\"puExport.txt\">file</a>";
 	}
 	return $sMsg;
 }
 
 /**
  * Importa i processi interpretando un file contenente un array serializzato
  */
 function puImp(){
 	global $objSDAO;
	$errMsg = "";
	$errMsgOut = "";
	$sLog = "";
	if(file_exists($_FILES["import_file"]["tmp_name"]) || $_FILES["import_file"]["size"] != 0){
		$sLog .= date("d/m/Y h:i:s")."\tinizio import\n";
		$sImport = file_get_contents($_FILES["import_file"]["tmp_name"]);
		$aPu = unserialize($sImport);
		/**
		echo("<pre>");
		var_dump($aPu);
		die("</pre>");
		*/
		//inserisco il processo
		foreach($aPu as $process_unit=>$aProcUnit){
			if(!isset($aProcUnit["process_unit"])){
				$errMsg = _t("Il file non contiene dati relativi a processi"); 
				$errMsgOut .= $errMsg."<br>";
				$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
				return $errMsg; 
			}
			$aTmp = explode(".", $process_unit);
			if(count($aTmp) != 3){
				$errMsg = _t("Il codice del processo non &egrave; nel formato atteso"); 
				$errMsgOut .= $errMsg."<br>";
				$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
				return $errMsg; 
			}
			//verifico se esistono modulo e sottomodulo a cui il processo appartiene
			$module = $aTmp[0];
			$sub_module = $aTmp[1];
			$sSQL = "SELECT count(*) AS num FROM module WHERE module='$module'";
			if($objSDAO->query($sSQL, "test")){
				$row = $objSDAO->fetch("test");
				if($row["num"] != 1){
					$errMsg = _t("Il modulo")." $module "._t("non esiste.")."<br/>"; 
					$errMsgOut .= $errMsg."<br>";
					$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
					return $errMsg; 
				}
			}
			$sSQL = "SELECT count(*) AS num FROM sub_module " .
					"WHERE module='$module' AND sub_module='$sub_module'";
			if($objSDAO->query($sSQL, "test")){
				$row = $objSDAO->fetch("test");
				if($row["num"] != 1){
					$errMsg = _t("Il sottomodulo")." $sub_module "._t("non esiste.")."<br/>"; 
					$errMsgOut .= $errMsg."<br>";
					$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
					return $errMsg; 
				}
			}
			$_REQUEST["process_unit"] = $aTmp[2];
			$_REQUEST["description"] = $aProcUnit["process_unit"]["description"];
			$_REQUEST["module"] = $aProcUnit["process_unit"]["module"];
			$_REQUEST["sub_module"] = $aProcUnit["process_unit"]["sub_module"];
			$_REQUEST["type"] = $aProcUnit["process_unit"]["type"];
			$_REQUEST["note"] = $aProcUnit["process_unit"]["note"];
			$errMsg = puIns(false);
			if($errMsg == ""){
				$sLog .= date("d/m/Y h:i:s")."\tInserito il processo $process_unit\n";		
			}else{
				$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
				$errMsgOut .= $errMsg."<br>";
				//return $errMsg; 
			}	
			//inserisco gli alias del processo
			if(isset($aProcUnit["aliases"])){
				$aPuAliases = $aProcUnit["aliases"];
				foreach($aPuAliases as $alias=>$aPuAlias){
					$_REQUEST["process_unit"] = $process_unit;
					$_REQUEST["alias"] = $alias;
					$_REQUEST["entity"] = $aPuAlias["entity"];
					$_REQUEST["description"] = $aPuAlias["description"];
					$_REQUEST["read_only"] = $aPuAlias["read_only"];
					$_REQUEST["note"] = $aPuAlias["note"];
					if(strtoupper($alias) < "W"){
						//verifico che la entity a cui punta l'alias esista
						//gli alias W X Y Z sono variabili di memoria.
						$sSQL = "SELECT count(*) AS num FROM entity WHERE entity='".$aPuAlias["entity"]."'";
						if($objSDAO->query($sSQL, "test")){
							$row = $objSDAO->fetch("test");
							if($row["num"] != 1){
								$errMsg = _t("L'entity")." ".$aPuAlias["entity"]." "._t("non esiste.")."<br/>"; 
								$errMsgOut .= $errMsg."<br>";
								$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
								//return $errMsg; 
							}
						}
					}
					$errMsg = puAliasIns(false);
					if($errMsg == ""){
						$sLog .= date("d/m/Y h:i:s")."\tInserito l'alias pu $alias'\n";		
					}else{
						$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
						$errMsgOut .= $errMsg."<br>";
						//break;
					}
					//inserisco le variabili collegate all'alias
					if(isset($aPuAlias["variables"])){
						$aPuVars = $aPuAlias["variables"];
						foreach($aPuVars as $variable=>$aPuVar){	
							$_REQUEST["process_unit"] = $process_unit;
							$_REQUEST["alias"]= $alias;
							$_REQUEST["variable"]= $variable;
							$_REQUEST["description"] = $aPuVar["description"];
							$_REQUEST["read_only"] = $aPuVar["read_only"];
							$_REQUEST["note"] = $aPuVar["note"];
							$errMsg = puVarIns(false);
							if($errMsg == ""){
								$sLog .= date("d/m/Y h:i:s")."\tInserito la variabile pu $variable\n";		
							}else{
								$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
								$errMsgOut .= $errMsg."<br>";
								//break;
							}
						}
					}
					//inserisco gli attributi collegati all'alias
					if(isset($aPuAlias["attributes"])){
						$aPuAttrs = $aPuAlias["attributes"];
						foreach($aPuAttrs as $attribute=>$aPuAtt){	
							$_REQUEST["process_unit"] = $process_unit;
							$_REQUEST["alias"] = $alias;
							$_REQUEST["entity"] = $aPuAtt["entity"];
							$_REQUEST["attribute"] = $aPuAtt["attribute"];
							$_REQUEST["description"] = $aPuAtt["description"];
							$_REQUEST["read_only"] = $aPuAtt["read_only"];
							$_REQUEST["note"] = $aPuAtt["note"];
							//verifico che l'attribute a cui punta l'alias esista
							$sSQL = "SELECT count(*) AS num FROM attribute " .
									"WHERE entity='".$aPuAtt["entity"]."' " .
											"AND attribute='".$aPuAtt["attribute"]."'";
							if($objSDAO->query($sSQL, "test")){
								$row = $objSDAO->fetch("test");
								if($row["num"] != 1){
									$errMsg = _t("L'attribute")." ".$aPuAtt["attribute"]." ". _t("delt&agrave;")." ".$aPuAtt["entity"]." "._t("non esiste."); 
									$errMsgOut .= $errMsg."<br>";
									$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
									//return $errMsg; 
								}
							}
							$errMsg = puAttrMod(false);
							if($errMsg == ""){
								$sLog .= date("d/m/Y h:i:s")."\tInserito l'attribute pu $attribute\n";		
							}else{
								$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
								$errMsgOut .= $errMsg."<br>";
								//break;
							}
						}
					}
				}
			}
			//inserisco i link
			if(isset($aProcUnit["links"])){
				$aLinks = $aProcUnit["links"];
				foreach($aLinks as $linkName=>$aLink){
					$_REQUEST["process_unit"] = $process_unit;
					$_REQUEST["name"] = $linkName;
					$_REQUEST["pu_linked"] = $aLink["pu_linked"];
					$_REQUEST["group"] = $aLink["group"];
					$_REQUEST["sequence"] = $aLink["sequence"];
					$_REQUEST["label"] = $aLink["label"];
					$_REQUEST["tooltip"] = $aLink["tooltip"];
					$_REQUEST["note"] = $aLink["note"];
					$errMsg = puLinkIns(false);
					if($errMsg == ""){
						$sLog .= date("d/m/Y h:i:s")."\tInserito il link $linkName\n";		
					}else{
						$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
						$errMsgOut .= $errMsg."<br>";
						//break;
					}
				}				
			}
			//inserisco i documenti
			if(isset($aProcUnit["docs"])){
				$aDocs = $aProcUnit["docs"];
				foreach($aDocs as $fileName=>$aDoc){
					//inserisco il doc se non esiste
					$sSQL = "SELECT count(*) AS num FROM process_unit_doc WHERE process_unit='$process_unit'";
					if($objSDAO->query($sSQL, "test")){
						$row = $objSDAO->fetch("test");
						if($row["num"] == 0){
							$sSQL = "INSERT INTO process_unit_doc 
									(process_unit, file_name, name, mime_type)
									VALUES
									('$process_unit', '$fileName', '".$aDoc["name"]."', '".$aDoc["mime_type"]."')";
							if(!$objSDAO->exec($sSQL, "insert")){
								$errMsg = $objSDAO->error."<br/>";
							}	
							if($errMsg == ""){
								$sLog .= date("d/m/Y h:i:s")."\tInserito documento $fileName [".$aDoc["name"]."]\n";		
							}else{
								$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
								$errMsgOut .= $errMsg."<br>";
							}	
						}
					}					
				}				
			}
			//inserisco i frames
			$aFrames = $aProcUnit["frames"];
			foreach($aFrames as $frame=>$aFrame){					
				$_REQUEST["process_unit"] = $process_unit;
				$_REQUEST["frame"] = $frame;
				$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
				$_REQUEST["description"] = $aFrame["description"];
				$_REQUEST["display_only"] = $aFrame["display_only"];
				$_REQUEST["note"] = $aFrame["note"];
				$_REQUEST["new_layout_set"] = "";
				$errMsg = frameIns(false);
				if($errMsg == ""){
					$sLog .= date("d/m/Y h:i:s")."\tInserito il frame $frame\n";		
				}else{
					$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
					$errMsgOut .= $errMsg."<br>";
					//break;
				}
				//inserisco il layout set
				$aTmp = explode(".", $aFrame["layout_set"]["layout_set"]);					
				//echo($aFrame["layout_set"]["layout_set"]." => ".$aTmp[1]);
				if(count($aTmp) != 2){
					$errMsg = _t("Il codice del layout_set non &egrave; nel formato atteso. Inserisco un layout set temporaneo."); 
					$errMsgOut .= $errMsg."<br>";
					$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
					//break;	
					$_REQUEST["module"] = $aFrame["layout_set"]["module"];
					$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
					if($aFrame["layout_set"]["description"] != "")
						$_REQUEST["description"] = $aFrame["layout_set"]["description"];
					else
						$_REQUEST["description"] = "------";
					$_REQUEST["type"] = $aFrame["layout_set"]["type"];
					if($aFrame["layout_set"]["max_colspan"] != "" && $aFrame["layout_set"]["max_colspan"] != 0)
						$_REQUEST["max_colspan"] = $aFrame["layout_set"]["max_colspan"];	
					else
						$aFrame["layout_set"]["max_colspan"] = 20;
					$_REQUEST["note"] = $aFrame["layout_set"]["note"];								
				}else{
					$_REQUEST["module"] = $aFrame["layout_set"]["module"];
					$_REQUEST["layout_set"] = $aTmp[1];
					if($aFrame["layout_set"]["description"] != "")
						$_REQUEST["description"] = $aFrame["layout_set"]["description"];
					else
						$_REQUEST["description"] = "------";
					$_REQUEST["type"] = $aFrame["layout_set"]["type"];
					if($aFrame["layout_set"]["max_colspan"] != "" && $aFrame["layout_set"]["max_colspan"] != 0)
						$_REQUEST["max_colspan"] = $aFrame["layout_set"]["max_colspan"];	
					else
						$aFrame["layout_set"]["max_colspan"] = 20;
					$_REQUEST["note"] = $aFrame["layout_set"]["note"];					
				}
				$errMsg = layoutSetIns(false);
				if($errMsg == ""){
					$sLog .= date("d/m/Y h:i:s")."\tInserito il layout_set ".$aFrame["layout_set"]["layout_set"]."\n";		
				}else{
					$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
					$errMsgOut .= $errMsg."<br>";
					//break;
				}
				
				//inserisco i layout collegati al layout_set
				$aLayouts = $aFrame["layout_set"]["layouts"];
				foreach($aLayouts as $layout=>$aLayout){
					$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
					$_REQUEST["layout"] = $layout;
					$_REQUEST["description"] = $aLayout["description"];
					$_REQUEST["flags"] = $aLayout["flags"];
					$_REQUEST["display_only"] = $aLayout["display_only"];
					$_REQUEST["note"] = $aLayout["note"];
					$errMsg = layoutIns(false);
					if($errMsg == ""){
						$sLog .= date("d/m/Y h:i:s")."\tInserito il layout $layout\n";		
					}else{
						$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
						$errMsgOut .= $errMsg."<br>";
						//break;
					}
					//inserisco le band che fanno parte del layout
					$aBands =$aLayouts[$layout]["bands"];
					foreach($aBands as $band=>$aBand){
						$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
						$_REQUEST["layout"] = $layout;
						$_REQUEST["band"] = $band;
						$_REQUEST["flags"] = $aBand["flags"];
						$_REQUEST["description"] = $aBand["description"];
						$_REQUEST["note"] = $aBand["note"];
						$errMsg = layoutBandIns(false);
						if($errMsg == ""){
							$sLog .= date("d/m/Y h:i:s")."\tInserito la band $band\n";		
						}else{
							$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
							$errMsgOut .= $errMsg."<br>";
						}
						//inserisco i campi che fanno parte della band
						$aFields =$aBand["fields"];
						foreach($aFields as $field=>$aFields){
							$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
							$_REQUEST["layout"] = $layout;
							$_REQUEST["band"] = $band;
							$_REQUEST["row"] = $aFields["row"];
							$_REQUEST["seq"] = $aFields["seq"];
							$_REQUEST["layout_alias"] = $aFields["layout_alias"];
							$_REQUEST["entity"] = $aFields["entity"];
							$_REQUEST["alias_entity"] = $aFields["layout_alias"]."_".$aFields["entity"];
							$_REQUEST["attribute"] = $aFields["attribute"];
							$_REQUEST["variable"] = $aFields["variable"];
							$_REQUEST['label_colspan'] = $aFields['label_colspan'];
							$_REQUEST['label'] = $aFields['label'];
							$_REQUEST['label_attr'] = $aFields['label_attr'];
							$_REQUEST['label_align'] = $aFields['label_align'];
							$_REQUEST["label_style"] = $aFields["label_style"];
							$_REQUEST['field_mode'] = $aFields['field_mode'];
							$_REQUEST['field_type'] = $aFields['field_type'];
							$_REQUEST['field_format'] = $aFields['field_format'];
							$_REQUEST['field_format_par'] = $aFields['field_format_par'];
							$_REQUEST['field_value_list'] = $aFields['field_value_list'];
							$_REQUEST['field_attr'] = $aFields['field_attr'];
							$_REQUEST['field_align'] = $aFields['field_align'];
							$_REQUEST["field_style"] = $aFields["field_style"];
							$_REQUEST["field_width"] = $aFields["field_width"];
							$_REQUEST["field_height"] = $aFields["field_height"];
							$_REQUEST["field_colspan"] = $aFields["field_colspan"];
							$_REQUEST['check_variable'] = $aFields['check_variable'];
							$_REQUEST['field_check'] = $aFields['field_check'];
							$_REQUEST['lookup_sep1'] = $aFields['lookup_sep1'];
							$_REQUEST['lookup_sep2'] = $aFields['lookup_sep2'];
							$_REQUEST['lookup_sep3'] = $aFields['lookup_sep3'];
							$_REQUEST['lookup_sep4'] = $aFields['lookup_sep4'];
							$_REQUEST['lookup_attr1'] = $aFields['lookup_attr1'];
							$_REQUEST['lookup_attr2'] = $aFields['lookup_attr2'];
							$_REQUEST['lookup_attr3'] = $aFields['lookup_attr3'];
							$_REQUEST['html_string'] = $aFields['html_string'];
							//verifico che la entity a cui punta il campo esista
							if($aPuAlias["entity"] != ""){
								$sSQL = "SELECT count(*) AS num FROM entity WHERE entity='".$aPuAlias["entity"]."'";
								if($objSDAO->query($sSQL, "test")){
									$row = $objSDAO->fetch("test");
									if($row["num"] != 1){
										$errMsg = _t("L'entity")." ".$aPuAlias["entity"]." "._t("non esiste."); 
										$errMsgOut .= $errMsg."<br>";
										$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n";
										//return $errMsg; 
									}
								}
							}
							$errMsg = layoutBandFieldIns(false);
							if($errMsg == ""){
								$sLog .= date("d/m/Y h:i:s")."\tInserito il field $band ".$aFields["row"]."\n";		
							}else{
								$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
								$errMsgOut .= $errMsg;
							}
						}
					}
				}
				//inserisco gli alias collegati al layout_set
				if(isset($aFrame["layout_set"]["aliases"])){
					$aAliases = $aFrame["layout_set"]["aliases"];
					foreach($aAliases as $alias=>$aAlias){
						$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
						$_REQUEST["alias"] = $alias;
						$_REQUEST["entity"] = $aAlias["entity"];
						//verifico che la entity a cui punta l'alias esista
						if($aPuAlias["entity"] != ""){
							$sSQL = "SELECT count(*) AS num FROM entity WHERE entity='".$aPuAlias["entity"]."'";
							if($objSDAO->query($sSQL, "test")){
								$row = $objSDAO->fetch("test");
								if($row["num"] != 1){
									$errMsg = _t("L'entity")." ".$aPuAlias["entity"]." "._t("non esiste.");
									$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
									$errMsgOut .= $errMsg."<br>";
									//return $errMsg; 
								}
							}
						}
						$errMsg = layoutSetAliasIns(false);
						if($errMsg == ""){
							$sLog .= date("d/m/Y h:i:s")."\tInserito l'alias $alias\n";		
						}else{
							$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
							$errMsgOut .= $errMsg."<br>";
						}
						//inserisco le variabili collegate all'alias
						if(isset($aAlias["variables"])){
							$aVars = $aAlias["variables"];
							foreach($aVars as $variable=>$aVar){
								$_REQUEST["layout_set"] = $aFrame["layout_set"]["layout_set"];
								$_REQUEST["alias"] = $alias;
								$_REQUEST["variable"] = $variable;
								$_REQUEST["length"] = $aVar["length"];
								$_REQUEST["decimals"] = $aVar["decimals"];
								$_REQUEST["domain"] = $aVar["domain"];
								$errMsg = layoutSetVarIns(false);
								if($errMsg == ""){
									$sLog .= date("d/m/Y h:i:s")."\tInserito la variabile ".$variable."\n";		
								}else{
									$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
									$errMsgOut .= $errMsg."<br>";
								}
							}
						}
						//inserisco i matching fra pu_alias e layout_alias
						if(isset($aAlias["matching"])){
							$aMatchings = $aAlias["matching"];
							$layout_set = $aFrame["layout_set"]["layout_set"];
							foreach($aMatchings as $aliasMatch=>$aMatching){
								//verifico che il matching non esista: solo se non esiste lo inserisco
								$sSQL = "SELECT count(*) AS num FROM frame_alias_match " .
										"WHERE process_unit='$process_unit' " .
										"AND frame='$frame' " .
										"AND layout_set='$layout_set' " .
										"AND layout_alias='$alias'";
								if($objSDAO->query($sSQL, "test")){
									$row = $objSDAO->fetch("test");
									if($row["num"] == 0){
										$sSQL = "INSERT INTO frame_alias_match " .
										"(process_unit, frame, alias, layout_alias, layout_set)" .
										"VALUES" .
										"('$process_unit', '$frame', '$aliasMatch', '$alias', '$layout_set')";
										if(!$objSDAO->query($sSQL, "insert")){
											$errMsg = $objSDAO->error;
										}
									}
								}								
							}		
							if($errMsg == ""){
								$sLog .= date("d/m/Y h:i:s")."\tInserito matching fra alias\n";		
							}else{
								$sLog .= date("d/m/Y h:i:s")."\tERRORE:\t$errMsg\n"; 
								$errMsgOut .= $errMsg."<br>";
							}
						}
					}				
				}
			}			
		}
		/** 
		echo("<pre>");
		var_dump($aPu);
		echo("</pre>");
		/** **/
		$sLog .= date("d/m/Y h:i:s")."\tfine import\n";		
	}
	$fh = fopen(SERVER_FS_ROOT."/import_log.txt", "wb");
	fwrite($fh, $sLog);
	fclose($fh);
	if($errMsgOut == "")
		$errMsgOut .= _t("Importreqtype terminata con successo")."<br>";
	$errMsgOut = "L'importreqtype ha prodotto alcuni warning.<br><a target=\"_BLANK\" href=\"".SERVER_URL_ROOT."/import_log.txt\">LOG</a>";
	return $errMsgOut;
 }
?>